home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / futils / futils~1 / src / shell13s.zoo / shell1.3 / lib / getugroups.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-06  |  1.7 KB  |  70 lines

  1. /* getugroups.c -- return a list of the groups a user is in
  2.    Copyright (C) 1990-1991 Free Software Foundation.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Written by David MacKenzie. */
  19.  
  20. #include <sys/types.h>
  21. #include <grp.h>
  22. #ifdef POSIX
  23. #if !defined(sun) && !defined(ultrix)
  24. #define GID_T gid_t
  25. #else
  26. #define GID_T int
  27. #endif
  28. #else
  29. struct group *getgrent ();
  30. #define GID_T int
  31. #endif
  32. #if defined(USG) || defined(STDC_HEADERS)
  33. #include <string.h>
  34. #else
  35. #include <strings.h>
  36. #endif
  37.  
  38. /* Like `getgroups', but for user USERNAME instead of for
  39.    the current process. */
  40.  
  41. int
  42. getugroups (maxcount, grouplist, username)
  43.      int maxcount;
  44.      GID_T *grouplist;
  45.      char *username;
  46. {
  47.   struct group *grp;
  48.   register char **cp;
  49.   register int count = 0;
  50.  
  51.   setgrent ();
  52.   while ((grp = getgrent ()) != 0)
  53.     for (cp = grp->gr_mem; *cp; ++cp)
  54.       if (!strcmp (username, *cp))
  55.     {
  56.       if (maxcount != 0)
  57.         {
  58.           if (count >= maxcount)
  59.         {
  60.           endgrent ();
  61.           return count;
  62.         }
  63.           grouplist[count] = grp->gr_gid;
  64.         }
  65.       count++;
  66.     }
  67.   endgrent ();
  68.   return count;
  69. }
  70.